package com.starblink.library.widget.wishlist

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import androidx.lifecycle.lifecycleScope
import com.apollographql.apollo3.api.Optional
import com.starblink.android.basic.config.PAGE_SIZE
import com.starblink.android.basic.ext.enableCorner
import com.starblink.android.basic.ext.toast
import com.starblink.android.basic.extension.dp2px
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.basic.apollo.ext.optional
import com.starblink.android.basic.util.bus.FlowBus
import com.starblink.android.basic.util.bus.FlowConst
import com.starblink.android.basic.widget.dialog.BaseBottomSheetFragment
import com.starblink.basic.route.RoutePage
import com.starblink.basic.util.log.YYLogUtils
import com.starblink.library.widget.databinding.DialogWishlistRenameBinding
import com.starblink.library.widget.wishlist.bean.WishlistRenameEntry
import com.starblink.rocketreserver.FetchWishListsByAccountIdV2Query
import com.starblink.rocketreserver.UpdateWishMutation
import com.starblink.rocketreserver.type.WishListUpdateRequest
import com.starblink.rocketreserver.type.WishListsReq
import kotlinx.coroutines.async
import kotlinx.coroutines.launch

/**
 *  心愿单重命名
 **/
class WishlistRenameBottomDialog : BaseBottomSheetFragment() {

    private lateinit var binding: DialogWishlistRenameBinding
    private var id: String? = null
    private var title: String? = null
    private var desc: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //弹窗类型
        id = arguments?.getString(RoutePage.ARG.ID)
        title = arguments?.getString(RoutePage.ARG.TITLE)
        desc = arguments?.getString(RoutePage.ARG.DESC)

        YYLogUtils.e("111111   ----- $id  $title $desc")
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        binding = DialogWishlistRenameBinding.inflate(layoutInflater)
        return super.onCreateDialog(savedInstanceState)
    }

    override fun getBindingRootView(): View = binding.root

    override fun initView() {
        setPeekPercent(400.0 / 812.0)
        binding.root.enableCorner(32.dp2px(), "top")
        binding.btnClose.setOnClickListener {
            dismiss()
        }

        binding.boardTitle.text = title
        binding.boardDesc.text = desc

        binding.boardTitle.editText.imeOptions = EditorInfo.IME_ACTION_NEXT
        binding.boardDesc.editText.imeOptions = EditorInfo.IME_ACTION_DONE

        binding.btnCreate.setOnClickListener {
            if (binding.boardTitle.text.isNullOrEmpty()) {
                toast("title is not empty")
            } else {
                lifecycleScope.async {
                    FlowBus.with<WishlistRenameEntry>(FlowConst.WISH_DETAILS_CHANGE)
                        .post(
                            WishlistRenameEntry(
                                id = id!!,
                                title = binding.boardTitle.text,
                                desc = binding.boardDesc.text
                            )
                        )
                }
                dismiss()
            }
        }
    }


    companion object {
        @JvmStatic
        fun showWishlistRenameDialog(
            context: Activity,
            id: String,
            title: String,
            desc: String
        ) {
            (context as? AppCompatActivity?)?.let {
                WishlistRenameBottomDialog().apply {
                    arguments = bundleOf(
                        RoutePage.ARG.ID to id,
                        RoutePage.ARG.TITLE to title,
                        RoutePage.ARG.DESC to desc,
                    )
                }.show(it.supportFragmentManager, "WishlistRenameBottomDialog")
            }
        }
    }
}